{include mobile_template('header')}
<link rel="stylesheet" href="{URLROOT}/{$_G[tplurl_mobile]}/css/tuan.css">
<!--{eval $myprice = $detail['status']=='succeeded' ? $detail['real_price'] : $detail['price'];}-->
<div class="atab-box">

    <div class="atab-box-area">

        <div class="atab-box-area-dispay">
            <div class="panel-comm tuan-image-group">
                {if $detail[picture]}
                    <div class="tuan-image-heading">
                        <img src="{img $detail[picture]}" alt="$detail[subject]">
                    </div>
                {else}
                <div class="tuan-image-heading">
                    <img src="{img static/images/noimg.gif}" />
                </div>
                {/if}
            </div>
            <div class="panel-comm panel-default panel-fullsize">
                <ul class="list-group list-group-insertmod">
                    <li class="list-group-item tuan-detail-subject">
                        <b class="label-big">$detail[subject]</b>&nbsp;{if $detail[mode]!='normal'}<span class="label-comm label-mycolor">{print lang('tuan_mode_'.$detail[mode])}</span>{/if}
                    </li>
                     <li class="list-group-item list-noborder">
                        <div class="fr">
                            <span class="txt-comm txt-small txt-grey">
                                剩余：<span id="stock">{print $detail[goods_total]-$detail[goods_sell]}</span> 件
                            </span>
                        </div>
                        <b class="txt-red txt-big">&#165;{print get_numeric($myprice)}</b>
                        <s class="txt-grey txt-small">&#165;{print get_numeric($detail[market_price])}</s>
                    </li>
                    <li class="list-group-item">
                        <div class="fr">{date $detail[endtime],'Y-m-d H:i'}</div>
                        结束时间
                    </li>
                    <a class="list-group-item" href="{url tuan/mobile/do/detail/id/$detail[tid]/content/1}">
                        <div class="fr">
                            <span class="arrow"></span>
                        </div>
                        图文详情
                    </a>
                </ul>
                <div class="panel-body">
                    <a class="btn btn-mycolor block" href="{url tuan/mobile/do/buy/id/$detail[tid]}">立刻购买</a>
                </div>

                <div class="list-group list-group-insertmod mb10">
                    <a href="{url product/mobile/do/shop/sid/$subject[sid]}" class="list-group-item">
                        <div class="fr">
                            <span class="arrow"></span>
                        </div>
                        $subject[name]{if $subject[subname]}($subject[subname]){/if}
                        {if $subject[address]||$subject[c_address]}
                        <p class="txt-comm txt-small txt-grey">
                            地址:
                            $subject[address]
                            $subject[c_address]
                        </p>
                        {/if}
                    </a>
                    <!--{if $detail['mode'] == 'average'}-->
                    <div class="list-group-item">
                        <!--{eval $maxpeople = round($detail[goods_total]/$detail[people_buylimit]);}-->
                        本次团购为<span class="txt-red">平摊团购</span>模式：当前团购产品的总价格<span class="txt-red">{$detail[total_price]}元</span>，下单时，每位顾客均以<span class="txt-red">{$detail[price]}元</span>的价格购买，当团购人数达到<span class="txt-red">{$detail[peoples_min]}</span>人时，团购成功；在团购结束后，每位顾客最终得到的价格 = 产品的总价格 ÷ 实际购买的总人数，如果本次团购最终有<span class="txt-red">{$maxpeople}</span>人成功购买，按照平摊价格模式，每位用户最终只需花费<span class="txt-red">{print round($detail[total_price]/$maxpeople, 2)}元</span>即可。团购结束后，如果最终价格少于下单时的价格，网站会退还多余的钱到您的帐号内。
                    </div>
                    <!--{elseif $detail['mode'] == 'wholesale'}-->
                    <div class="list-group-item">
                        本次团购为<span class="txt-red">批发团购</span>模式：当前每件产品价格为<span class="txt-red">{$detail[price]}元</span>，当本次团购累计销售<span class="txt-red">{$detail[goods_min]}件</span>产品时，即团购成功！当本次团购结束时，最后累计的销售数量，根据批发数量和价格表进行计算，最后得到每件产品的最终价格，如果最终每件产品的价格低于下单时的价格，网站将退还多出来的差价到用户帐号。以下为数量和价格表：
                        <!--{eval $prices = explode(',', $detail['prices']);}-->
                        <p>
                        <!--{loop $prices $_pri}-->
                        <!--{eval list($_n,$_p)=explode('=', $_pri);}-->
                        <div>销售数量大于<span class="txt-red">$_n</span>件产品时，最终单价为<span class="txt-red">$_p</span>元/件；</div>
                        <!--{/loop}-->
                        </p>
                    </div>
                    <!--{elseif $detail['mode'] == 'forestall'}-->
                    <div class="list-group-item">
                        本次团购为<span class="txt-red">抢鲜团购</span>模式：当前团购下单价格为<span class="txt-red">{$detail[price]}元</span>，当本次团购累计购买人数打到<span class="txt-red">{$detail[peoples_min]}人</span>时，即团购成功！<br>下单越早的人价格越低，当本次团购结束时，根据最终团购价，网站将退还多出来的差价到用户帐号。以下为价格策略：
                        <!--{eval $prices = explode(',', $detail['prices']);}-->
                        <p>
                        <!--{eval $_x=1;}-->
                        <!--{loop $prices $_pri}-->
                        <!--{eval
                            list($_n, $_p) = explode('=', $_pri);
                            $_xx = $_n == $_x ? $_n : ($_x . '-' . $_n);
                            $_x = $_n+1;
                        }-->
                        <div>第 {$_xx} 位下单的用户，最终团购价格为<span class="txt-red">$_p</span>元；</div>
                        <!--{/loop}-->
                        <div>从 {$_x} 位开始下单的用户，最终团购价格为<span class="txt-red">{$detail[price]}</span>元；</div>
                        </p>
                    </div>
                    <!--{/if}-->
                </div>
            </div>
        </div>

    </div>

</div>
<div class="panel-comm panel-default none" id="buy_box">
    <div class="panel-heading">购买选项</div>
    <div class="panel-body">
        <div class="form-box">
            <div class="form-item">
                <label>购买数量:</label>
                <input type="number" name="quantity" s placeholder="请输入购买数量" value="1" min="1" max="$product[stock]" style="width: 100%;">
                <div class="clear"></div>
            </div>
            <div class="form-submit">
                <div class="btn-group btn-group-auto">
                    <div class="btn-group">
                        <button type="button" class="btn btn-mycolor" data-type="submit">提交</button>
                    </div>
                    <div class="btn-group">
                        <button type="button" class="btn btn-default" data-type="close">关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
$(document).ready(function() {

});

var cart = function() {
    var id = Number($('#product_id').val()); //产品ID
    var stock = Number($('#product_stock').val()); //库存
    var style = Number($('#product_style').val()); //类型1：实物2：虚拟
    var btn_type = ''; //once or cart

    function add_cart_callback(cid, quantity, buyattr) {
        if(btn_type=='once') {
            jslocation(Url('product/mobile/do/order/cids/'+cid));
        } else {
            stock -= quantity;
            $('#stock').val(stock);
            $('#product_stock').text(stock);
            if(window.confirm('成功将该商品加入购物车！是否进入购物车页面？')) {
                jslocation(Url('product/mobile/do/cart'));
            } else {
                mdrawer.close();
            }
        }
    }

    //加入到购物车
    function add_cart(quantity, buyattr) {
        if(1 > stock) {
            alert('无法添加到购物车，产品库存不足。');
            return;
        }
        $.post(Url('product/cart/op/add'), {'pid':id, buyattr:buyattr, 'num':quantity, in_ajax:1 }, 
        function(result) {
            if(is_message(result)) {
                myAlert(result);
            } else if(is_numeric(result.trim())) {
                var cid = result.trim();
                add_cart_callback(cid);
            } else {
                console.debug(result);
                alert('产品读取失败，可能网络忙碌，请稍后尝试。');
            }
        });
    }

    mdrawer = new $.m_drawer('#buy_box', {}, {
        //提交对话框表单
        onSubmit:function(obj, applybox) {
            var quantity = parseInt(applybox.find('input[name="quantity"]').val());
            if(!is_numeric(quantity) || quantity < 0) {
                alert('未填写购买数量。');
            } else if(quantity > stock) {
                alert('库存不足，商品库存仅剩'+stock+'件。');
            } else {
                var buyattr = '';
                applybox.find('[data-type="buyattr_select"]').each(function() {
                    var _select = $(this);
                    buyattr += (buyattr?';':'')+_select.data('id')+':'+_select.val();
                });
                add_cart(quantity, buyattr);
            }
            return false;
        }
    });

    $('#add_cart_btn').click(function(event) {
        event.preventDefault();
        btn_type = 'cart';
        mdrawer.open();
    });
    $('#once_btn').click(function(event) {
        event.preventDefault();
        btn_type = 'once';
        mdrawer.open();
    });
};
</script>
{include mobile_template('footer')}